﻿using System;
using System.Windows;
using System.Windows.Input;
using EzDesk.DesktopClient.FunctionWindowModule;
using EzDesk.DesktopClient.Storage;
using EzDesk.DesktopClient.UtilityView.View;
using EzDesk.DesktopClient.UtilityView.ViewContract;
using EzDesk.DesktopClient.UtilityView.ViewModel;
using EzDesk.DesktopClient.View;
using EzDesk.DesktopClient.ViewModel;
using EzDesk.DesktopClient.Event;
using EzDesk.DesktopClient.ViewContract;
using EzDesk.Generic;
using EzDesk.Generic.WPF;
using System.Drawing;
using EzDesk.DesktopClient.AppModule;
using EzDesk.Utility.Net;
using Microsoft.Practices.Unity;
using Microsoft.Win32;

namespace EzDesk.DesktopClient
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class IMClient : Application
    {
        private static void SetAutoRun()
        {
            //开机自启动
            string startpath = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) + "\\Programs\\EzDesk\\上海魏玛\\易得桌面.appref-ms";
            RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            string sRtn = key.GetValue("ezdesk", "notexist").ToString();
            if (sRtn == "notexist")
            {
                key.SetValue("ezdesk", startpath);
            }
            else
            {
                if (sRtn != startpath)
                    key.SetValue("ezdesk", startpath);
            }
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            try
            {
                base.OnStartup(e);
                SetAutoRun();

                this
                    .AddResources()
                    .ConfigManagement()
                    .RegistViews()
                    .InitModules()
                    .InitViewModel()
                    .SetTheme()
                    .StartShow()
                    .InitShortcut();
            }
            catch (Exception exp)
            {
                LogEx.ERROR("DesktopClient", exp.Message, exp);
                System.Windows.MessageBox.Show(exp.Message);
            }
        }

        private IMClient SetTheme()
        {
            ThemeSelector.SetCurrentThemeDictionary(MainWindow, new Uri("/EzDesk.DesktopClient;component/Theme/GrayTheme.xaml", UriKind.Relative));
            return this;
        }

        private IMClient AddResources()
        {
            return this;
        }

        public IMClient RegistViews()
        {
            IMClientUnityContainer.Instance.RegisterType<IWebRequestExt, WebRequestExt>();
            //            IMClientUnityContainer.Instance.RegisterType<IFeedItemView, FeedItemView>();
            IMClientUnityContainer.Instance.RegisterType<IFeedView, FeedView>();
            IMClientUnityContainer.Instance.RegisterType<ILoginView, LoginView>();
            IMClientUnityContainer.Instance.RegisterType<ISelfInfoView, SelfInfoView>();
            IMClientUnityContainer.Instance.RegisterType<IShellView, ShellView>();
            IMClientUnityContainer.Instance.RegisterType<ITopPanelView, TopPanelView>();
            IMClientUnityContainer.Instance.RegisterType<IContentTabView, ContentTabView>();
            //            IMClientUnityContainer.Instance.RegisterType<INotificationView, NotificationView>();
            IMClientUnityContainer.Instance
                .RegisterType<INotificationItemView, NotificationItemView>()
                .RegisterType<INotificationItemViewModel, NotificationItemViewModel>();
           

            IMClientUnityContainer.Instance
                .RegisterType<IUpdateItemView, UpdateItemView>()
                .RegisterType<IUpdateItemViewModel,UpdateItemViewModel>();
            IMClientUnityContainer.Instance
                .RegisterType<ITodoView, TodoView>()
                .RegisterType<TodoViewModel,TodoViewModel>()
                .RegisterType<ITodoItemViewModel, TodoItemViewModel>();

            IMClientUnityContainer.Instance.RegisterType<ILoginViewModel, LoginViewModel>();
            IMClientUnityContainer.Instance.RegisterType<ITopPanelViewModel, TopPanelViewModel>();

            IMClientUnityContainer.Instance.RegisterType<ISelfInfoViewModel, SelfInfoViewModel>();
            IMClientUnityContainer.Instance.RegisterType<IContentTabViewModel, ContentTabViewModel>();
            IMClientUnityContainer.Instance.RegisterType<IActionViewModel, ActionViewModel>();


            IMClientUnityContainer.Instance.RegisterType<IShellViewModel, ShellViewModel>();


            return this;
        }

        private IMClient InitModules()
        {
            new FunctionWindowModuleInfo()
                   .Initialize();

            new AppModuleInfo()
                .Initialize();

            new PopupModule.PopupModuleInfo()
                .Initialize();

            //var optionsModuleInfo = new OptionsModule.OptionsModuleInfo();
            //optionsModuleInfo.Initialize();

            //var messageModuleInfo = new MessageModule.MessageModuleInfo();
            //messageModuleInfo.Initialize();
            return this;
        }

      
        protected IMClient InitViewModel()
        {
            var viewModel = IMClientUnityContainer.Instance.Resolve<IShellViewModel>();
            this.MainView = viewModel.View as Window;
            return this;
        }

        private IMClient ConfigManagement()
        {
            ClientConfigurationManagement = ClientConfigurationManagement.Instance;
            ServiceConfigurationManagement = ServiceConfigurationManagement.Instance;
            UserManagement = UserManagement.Instance;
            DataManagement = DataManagement.Instance;
            UpdateDataManagement = UpdateDataManagement.Instance;
            AppManagement = AppManagement.Instance;
            TodoManagement = TodoManagement.Instance;
            return this;
        }

        protected IMClient StartShow()
        {
            //UserManagement.Login("hg", "123456");
            this.MainView.Show();
            this.MainView.ToBesideScreen(ContentAlignment.MiddleCenter);
            CacheStorage.Instance.Set("MainWindow", MainView);
            return this;
        }

        private IMClient InitShortcut()
        {
            EventManager.RegisterClassHandler(typeof(Window), Keyboard.KeyDownEvent, new KeyEventHandler(KeyUp), true);
            return this;
        }


        private void KeyUp(object sender, KeyEventArgs e)
        {
            //Exit
            if (e.Key == Key.F9 && Keyboard.Modifiers == ModifierKeys.Control)
                Shutdown();

            //Logout
            if (e.Key == Key.F11 && Keyboard.Modifiers == ModifierKeys.Control)
                LogoutEvent.Instance.Publish(new LogoutArgs());

            //Stop getting data from server
            if (e.Key == Key.F12 && Keyboard.Modifiers == ModifierKeys.Control)
                DataManagement.Instance.Stop();
        }

        protected override void OnExit(ExitEventArgs e)
        {
            ShutDownEvent.Instance.Publish(null);
            base.OnExit(e);
        }
    }
}
